package com.ebeijia.service.admin.impl;

import com.ebeijia.common.FileOperation;
import com.ebeijia.context.RequestResponseContext;
import com.ebeijia.dto.ApplyWaterDto;
import com.ebeijia.exception.ServiceException;
import com.ebeijia.mybatis.mapper.NoticeInfoMapper;
import com.ebeijia.mybatis.pojo.NoticeInfo;
import com.ebeijia.myenum.ApplyWaterSteteEnum;
import com.ebeijia.myenum.NoticeInfoTypeEnum;
import com.ebeijia.query.NoticeInfoQuery;
import com.ebeijia.service.admin.NoticeInfoService;
import com.ebeijia.util.LoggerUtil;
import com.ebeijia.util.PropertiesUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by YPJ on 2016/5/18.
 */
@Service
public class NoticeInfoServiceImpl implements NoticeInfoService {
    @Resource
    private NoticeInfoMapper noticeInfoMapper;

    @Override
    public List<NoticeInfo> selectAllNotice(NoticeInfoQuery noticeInfoQuery) {
        List<NoticeInfo> noticeInfos = new ArrayList<>();
        noticeInfos = noticeInfoMapper.selectAllNotice(noticeInfoQuery);
        for (NoticeInfo noticeInfo : noticeInfos) {

            noticeInfo.setNoticeBusType(NoticeInfoTypeEnum.text(noticeInfo.getNoticeBusType()));
        }
        return noticeInfos;
    }

    @Override
    public int selectAllNoticeCount(NoticeInfoQuery noticeInfoQuery) {
        return noticeInfoMapper.selectAllNoticeCount(noticeInfoQuery);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return noticeInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertNoticeInfo(NoticeInfo noticeInfo)
        throws ServiceException {

        try {
            noticeInfo.setNoticeCreateDate(new Date());
            noticeInfo.setNoticeDepDate(new Date());
            noticeInfo.setNoticeDepOpr(RequestResponseContext.getUserContext().getAccount());
            noticeInfo.setNoticeCreateOpr(RequestResponseContext.getUserContext().getAccount());
            noticeInfo.setNoticeStatus("01");
            String filename = UUID.randomUUID().toString();
            /*  String path = PropertiesUtils.getProperties("txt.upload.path ")+filename;*/
            String path = FileOperation.createFile(filename);
            FileOperation.writeTxtFile(noticeInfo.getDescription(), path);
            noticeInfo.setNoticeConPath(path);

        } catch (Exception e) {
            throw new ServiceException(e);
        }

        return noticeInfoMapper.insertSelective(noticeInfo);
    }

    @Override
    public NoticeInfo selectByPrimaryKey(String id)
        throws Exception {
        NoticeInfo noticeInfo = noticeInfoMapper.selectByPrimaryKey(id);
        String content = FileOperation.readTxtFile(noticeInfo.getNoticeConPath());
        noticeInfo.setDescription(content);
        return noticeInfo;
    }

    @Override
    public int updateByPrimaryKey(NoticeInfo noticeInfo)
        throws Exception {
        FileOperation.writeTxtFile(noticeInfo.getDescription(), noticeInfo.getNoticeConPath());
        return noticeInfoMapper.updateByPrimaryKeySelective(noticeInfo);
    }

    @Override
    public int updateByPrimaryKeySelective(String id) {
        NoticeInfo noticeInfo = new NoticeInfo();
        noticeInfo.setId(id);
        noticeInfo.setNoticeStatus("02");
        noticeInfo.setNoticeDepDate(new Date());
        return noticeInfoMapper.updateByPrimaryKeySelective(noticeInfo);
    }

}
